package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.SPNursingHome.po.CareItem;
import com.neusoft.SPNursingHome.po.LevelItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LevelItemDao extends BaseMapper<LevelItem> {
    // 根据护理级别ID查询关联的护理项目ID列表
    @Select("SELECT care_item_id FROM level_item WHERE care_level_id = #{careLevelId}")
    List<Integer> selectCareItemIdsByCareLevelId(String careLevelId);

    Integer insertBatchSomeColumn(List<LevelItem> levelItems);

    // 查询状态为1且护理级别为careLevelId的护理项目
    @Select("SELECT * FROM care_item WHERE care_item_id IN " +
            "(SELECT care_item_id FROM level_item WHERE care_level_id = #{careLevelId}) " +
            "AND care_item_label = 1 AND care_item_status = 1")
    List<CareItem> selectCareItemsByCareLevelIdNP(Integer careLevelId);

    // 分页功能，查询状态为1且护理级别为careLevelId的护理项目
    @Select("SELECT * FROM care_item WHERE care_item_id IN " +
            "(SELECT care_item_id FROM level_item WHERE care_level_id = #{careLevelId}) " +
            "AND care_item_label = 1 AND care_item_status = 1 " +
            "LIMIT #{offset}, #{limit}")
    List<CareItem> selectCareItemsByCareLevelId(@Param("careLevelId") Integer careLevelId,
                                                @Param("offset") long offset,
                                                @Param("limit") long limit);

    // 删除护理级别下的护理项目
    @Delete("DELETE FROM level_item WHERE care_level_id = #{careLevelId} AND care_item_id = #{careItemId}")
    int deleteLevelItem(@Param("careLevelId") Integer careLevelId, @Param("careItemId") Integer careItemId);

    // 辅助分页功能，计算total
    @Select("SELECT COUNT(*) FROM care_item WHERE care_item_id IN " +
            "(SELECT care_item_id FROM level_item WHERE care_level_id = #{careLevelId}) " +
            "AND care_item_label = 1 AND care_item_status = 1")
    int countCareItemsByCareLevelId(@Param("careLevelId") Integer careLevelId);
}
